package com.apollow.oschina.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.apollow.oschina.R;
import com.apollow.oschina.adapter.HotTweetAdapter;
import com.apollow.oschina.base.BaseFragment;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.TweetBean;
import com.apollow.oschina.protocol.HotTweetProtocol;
import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
/**
 * Created by Administrator on 2017/1/18.
 * 项目名:    OsChina
 * 包名:      com.apollow.oschina.fragment
 * 文件名:    HotTweetFragment
 * 创建者:    LXF
 * 创建时间:   ${DATA}15:27
 * 描述:     TODO
 */
public class HotTweetFragment extends BaseFragment {
    @BindView(R.id.recycleView)
    ListView mListView;
    @BindView(R.id.test_list_view_frame)
    PtrClassicFrameLayout mTestListViewFrame;
    private HotTweetProtocol mProtocol;
    private HotTweetAdapter mHotTweetAdapter;
    List<TweetBean> tweets = new ArrayList<>();
    public int index = -1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = Global.layoutInflate(R.layout.fragment_tweet);
        ButterKnife.bind(this, view);
        mHotTweetAdapter = new HotTweetAdapter(getActivity());
        mProtocol = new HotTweetProtocol();
        mListView.setAdapter(mHotTweetAdapter);
        mTestListViewFrame.setLoadMoreEnable(true);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        initData();
        initEvent();
    }
    private void initEvent() {
        mTestListViewFrame.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                index++;
                System.out.println(index);
                if (index>1){
                    Global.showToast("没有东西可以刷新了");
                    mTestListViewFrame.refreshComplete();
                    return;
                }
                tweets.clear();
                mProtocol.setId(String.valueOf(index));
                initData();
                mTestListViewFrame.refreshComplete();
            }
        });
        mTestListViewFrame.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
                index++;
                if (index >1) {
                    Global.showToast("已经没有更多了");
                    return;
                }
                mProtocol.setId(String.valueOf(index));
                initData();
                mTestListViewFrame.loadMoreComplete(true);
            }
        });
    }
    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                tweets.addAll(mProtocol.getData().getTweets());
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mHotTweetAdapter.setTweets(tweets);
                    }
                });
            }
        }).start();
    }

}
